"""
Case Type   : 基础功能-兼容B库语法-optimize
Case Name   : optimize与dml操作并发执行-通过java程序实现并发
Create At   : 2024/10/28
Owner       : peilinqian
Description :
    step1:创建指定数据库;
    step2:远程复制java脚本至指定主机;
    step3:编译java文件
    Java脚本实现：Optimize（300次）、dml（insert、update、delete各100次）两个线程并发执行;
    step4:运行java程序;
Expect      :
    step1:创建指定数据库; expect:创建成功
    step2:远程复制java脚本至指定主机; expect:复制成功
    step3:编译java文件; expect:编译成功
    step4:运行java程序; expect:执行结果正确
History     :
"""
import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class JDBCPrepareStatement(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info("----this is setup----")
        self.log.info(f'----{os.path.basename(__file__)}:start----')
        self.pri_dbuser = Node(node='PrimaryDbUser')
        self.pri_root = Node(node='PrimaryRoot')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.db_b_name = 'db_compatibility_optimize_case0005'
        self.tb_name = 't_compatibility_optimize_case0005'
        self.source_dir = os.path.join(macro.SCRIPTS_PATH, "compatibility")
        self.target_path = os.path.join(macro.DB_BACKUP_PATH,
                                        "Compatibility_Optimize_Case0005")
        self.java_name = "JDBC_Opengauss_Compatibility_Optimize_Case0005"
        self.com = Common()
        self.constant = Constant()

    def test_main(self):
        step_txt = '----step1:创建指定数据库; expect:创建成功----'
        self.log.info(step_txt)
        create_sql = f"drop database if exists {self.db_b_name};" \
                     f"create database {self.db_b_name} dbcompatibility 'B';" \
                     f"\c {self.db_b_name}"
        create_result = self.pri_sh.execut_db_sql(create_sql)
        self.log.info(create_result)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, create_result,
                      '执行失败' + step_txt)

        step_txt = '----step2:远程复制java脚本至指定主机; expect:复制成功----'
        self.log.info(step_txt)
        self.com.scp_file(self.pri_root,
                          f"{self.java_name}.java", self.target_path,
                          source_dir=self.source_dir)

        check_cmd = f'ls {self.target_path}'
        check_result = self.pri_root.sh(check_cmd).result()
        self.assertIn(self.java_name, check_result, '执行失败' + step_txt)

        step_txt = '----step3:编译java文件; expect:编译成功----'
        self.log.info(step_txt)
        jar_name, jar_path = self.com.get_jdbc('postgresql')
        self.pri_root.scp_put(jar_path,
                              f"{os.path.join(self.target_path, jar_name)}")
        javac_cmd = f"javac -encoding utf-8 -cp " \
                    f"{os.path.join(self.target_path, jar_name)} " \
                    f"{os.path.join(self.target_path, f'{self.java_name}.java')}"
        self.log.info(javac_cmd)
        javac_result = self.pri_root.sh(javac_cmd).result()
        self.log.info(javac_result)
        self.assertEqual('', javac_result, "执行失败" + step_txt)

        step_txt = '----step4:运行java程序; expect:执行结果正确----'
        self.log.info(step_txt)
        url_host_port = f"{self.pri_dbuser.db_host}:{self.pri_dbuser.db_port}"
        java_cmd = f"java -cp " \
                   f"{os.path.join(self.target_path, jar_name)}" \
                   f":{self.target_path} {self.java_name} {url_host_port} " \
                   f"{self.db_b_name} {self.pri_dbuser.db_user} " \
                   f"{self.pri_dbuser.db_password} {self.tb_name}"
        self.log.info(java_cmd)
        java_result = self.pri_root.sh(java_cmd).result()
        self.log.info(java_result)
        self.assertEqual(java_result.count('optimize 成功'), 300,
                         "执行失败" + step_txt)
        self.assertEqual(java_result.count('update 成功'), 100,
                         "执行失败" + step_txt)
        self.assertEqual(java_result.count('insert 成功'), 100,
                         "执行失败" + step_txt)
        self.assertEqual(java_result.count('delete 成功'), 100,
                         "执行失败" + step_txt)

    def tearDown(self):
        self.log.info('----this is teardown----')
        step_txt1 = '----删除普通库及B库; expect:删除成功----'
        self.log.info(step_txt1)
        drop_sql = f"drop database if exists {self.db_b_name};"
        drop_result = self.pri_sh.execut_db_sql(drop_sql)
        self.log.info(drop_result)
        step_txt2 = '----清理编译脚本; expect:清理成功----'
        self.log.info(step_txt2)
        file_rm_cmd = f'rm -rf {self.target_path};' \
                      f'if [ -d {self.target_path} ]; ' \
                      f'then echo "exists"; else echo "not exists"; fi'
        self.log.info(file_rm_cmd)
        file_rm_result = self.pri_root.sh(file_rm_cmd).result()
        self.log.info(file_rm_result)

        self.log.info('----清理环境断言----')
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, drop_result,
                      '执行失败' + step_txt1)
        self.assertEqual('not exists', file_rm_result, "执行失败" + step_txt2)

        self.log.info(f'----{os.path.basename(__file__)}:end----')
